const JwtStrategy = require('passport-jwt').Strategy,
  ExtractJwt = require('passport-jwt').ExtractJwt;
const db = require("./db");
const keys = require("./keys");
const opts = {}
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = keys.secretOrKey;

module.exports = passport => {
  passport.use(new JwtStrategy(opts, (jwt_payload, done) => {
    db.query(`SELECT * FROM users WHERE id='${jwt_payload.id}'`, (err, result) => {
      if (err) throw err;
      if (result) {
        return done(null, result)
      }
      return done(null, false)
    });
  }));
}